package com.lace.security.jwt.auth;

import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.security.web.util.matcher.OrRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by WangZX on 2018/7/14.
 */
public class JwtAuthenticationRequestMatcher implements RequestMatcher {

    private OrRequestMatcher skips;
    private OrRequestMatcher processes;

    public JwtAuthenticationRequestMatcher(List<String> pathsToSkip, List<String> processingPath) {
        List<RequestMatcher> skip = new ArrayList<>();
        for (String path : pathsToSkip) {
            skip.add(new AntPathRequestMatcher(path));
        }
        skips = new OrRequestMatcher(skip);

        List<RequestMatcher> process = new ArrayList<>();
        for (String path : processingPath) {
            process.add(new AntPathRequestMatcher(path));
        }
        processes = new OrRequestMatcher(process);
    }

    @Override
    public boolean matches(HttpServletRequest request) {
        if (skips.matches(request)) {
            return false;
        }
        return processes.matches(request) ? true : false;
    }
}
